// import 'dart:ffi';
import 'dart:io';
// import 'dart:typed_data';

// import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
// import 'package:win32/win32.dart';

class PlatformService {
  /// http://peeingtube.org/topic/details/useeffect-hook-problem-array-based-on-fetch-api-is-not-populated/22849
  /// https://github.com/dart-windows/win32/issues/657
  static IconData? extractAssociatedIcon(String filePath) {
    if (Platform.isWindows) {
      // final path = TEXT(filePath);

      // Pointer<SHFILEINFO> sfi = Pointer.fromAddress(0);
      // SHGetFileInfo(path, -1, sfi, sizeOf(sfi), 0x000000100 | 0x000000200);

      // ExtractIconEx()

      // final hwnd = ExtractAssociatedIcon(
      // GetModuleHandle(nullptr), path, allocator.  .allocate<Uint32>());

      // final hdc = GetDC(hwnd);
      // DrawIcon(hdc, X, Y, icon);
      // ReleaseDC(hwnd, hdc);

      // https://www.brad-smith.info/blog/archives/164
      // Pointer<SHFILEINFO> psfi = Pointer.fromAddress(0);
      // SHGetFileInfo(path, 0, psfi, sizeOf<SHFILEINFO>(), 0x1);
    }

    return null;
  }

  /// Extracts the icon from the given [path] and returns the bytes of the
  /// created BMP.
  ///
  /// To display the image, use the [Image.memory] widget:
  /// ```dart
  /// Image.network(getEncodedImageBytes(r'c:\windows\regedit.exe')!);
  /// ```
  // static Uint8List? getEncodedImageBytes(String path) {
  //   final hInstance = GetModuleHandle(nullptr);
  //   final pLocation = path.toNativeUtf16();
  //   final piIcon = calloc<Uint16>();
  //   final hIcon = ExtractAssociatedIcon(hInstance, pLocation, piIcon);

  //   if (hIcon == NULL) {
  //     return null;
  //   }

  //   final pIconInfo = calloc<ICONINFO>();

  //   if (GetIconInfo(hIcon, pIconInfo) == FALSE) {
  //     return null;
  //   }

  //   final bmp = calloc<BITMAP>();

  //   if (GetObject(pIconInfo.ref.hbmColor, sizeOf<BITMAP>(), bmp) == NULL) {
  //     return null;
  //   }

  //   final bitmapFileHeader = calloc<BITMAPFILEHEADER>();

  //   final bitmapInfoHeader = calloc<BITMAPINFOHEADER>()
  //     ..ref.biSize = sizeOf<BITMAPINFOHEADER>()
  //     ..ref.biWidth = bmp.ref.bmWidth
  //     ..ref.biHeight = bmp.ref.bmHeight
  //     ..ref.biPlanes = 1
  //     ..ref.biBitCount = 32
  //     ..ref.biCompression = BI_RGB;

  //   final dwBmpSize =
  //       ((bmp.ref.bmWidth * bitmapInfoHeader.ref.biBitCount + 31) /
  //               32 *
  //               4 *
  //               bmp.ref.bmHeight)
  //           .toInt();

  //   final hdc = GetDC(NULL);
  //   final lpBitmap = calloc<Uint8>(dwBmpSize);

  //   GetDIBits(hdc, pIconInfo.ref.hbmColor, 0, bmp.ref.bmHeight, lpBitmap,
  //       bitmapInfoHeader.cast(), DIB_RGB_COLORS);

  //   final dwSizeOfDIB =
  //       dwBmpSize + sizeOf<BITMAPFILEHEADER>() + sizeOf<BITMAPINFOHEADER>();

  //   bitmapFileHeader.ref.bfOffBits =
  //       sizeOf<BITMAPFILEHEADER>() + sizeOf<BITMAPINFOHEADER>();

  //   bitmapFileHeader.ref.bfSize = dwSizeOfDIB;
  //   bitmapFileHeader.ref.bfType = 0x4D42; // BM

  //   final bytes = (BytesBuilder()
  //         ..add(Pointer<Uint8>.fromAddress(bitmapFileHeader.address)
  //             .asTypedList(sizeOf<BITMAPFILEHEADER>()))
  //         ..add(Pointer<Uint8>.fromAddress(bitmapInfoHeader.address)
  //             .asTypedList(sizeOf<BITMAPINFOHEADER>()))
  //         ..add(lpBitmap.asTypedList(dwBmpSize)))
  //       .toBytes();

  //   DeleteObject(pIconInfo.ref.hbmColor);
  //   DeleteObject(pIconInfo.ref.hbmMask);
  //   DestroyIcon(hIcon);
  //   ReleaseDC(NULL, hdc);

  //   free(pIconInfo);
  //   free(bmp);
  //   free(pLocation);
  //   free(piIcon);
  //   free(bitmapFileHeader);
  //   free(bitmapInfoHeader);
  //   free(lpBitmap);

  //   return bytes;
  // }
}
